package com.mms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.mms.entity.Meeting;
import com.mms.entity.MinutesString;
import com.mms.util.HibernateUtil;


public class MinutesDao {
	
	public List<MinutesString> getMinStringMes(String topic){
		Session session = HibernateUtil.getSession();
		List<Object[]> objs;
		List<MinutesString> minStringList = new ArrayList<MinutesString>();
		try{
			String sql = "select * from minutes_string where topic=?";
			Transaction transaction = session.beginTransaction();
			Query query = session.createSQLQuery(sql);
			query.setString(0,topic);
			objs = query.list();
			for(Object[] objects : objs){
				MinutesString minutes = new MinutesString();
				Meeting meeting = new Meeting();
				minutes.setMinId(Integer.parseInt(objects[0].toString()));
				minutes.setMinString(objects[1].toString());
				minutes.setMinSubmitTime(Timestamp.valueOf(objects[2].toString()));
				meeting.setTopic(objects[3].toString());
				minutes.setMeeting(meeting);
				minStringList.add(minutes);
			}
			transaction.commit();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			session.close();
		}
		return minStringList;
	}
	
	/*
	public List<MinutesString> getMinBlobMes(String topic){
		Connection conn = new dbConnection().getconn();
		List<MinutesString> minBlob = new ArrayList<MinutesString>();
		try {
			String sql = "select * from minutes_document where topic=?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1,topic);
			ResultSet rSet = ps.executeQuery();
			while(rSet.next()){
				MinutesString minutes = new MinutesString();
				minutes.setMinID(rSet.getInt("m_id"));
				minutes.setMinByte(rSet.getByte("min_doc"));
				minutes.setSubTime(rSet.getString("min_time").toString());
				minutes.setTopic(topic);
				minBlob.add(minutes);
			}
			ps.close();
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return minBlob;
	}
	*/
	
	public void addMinutesStr(MinutesString minute){
		Session session = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			session.save(minute);
			transaction.commit();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			session.close();
		}
	}
}
